package com.estate.struts.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.estate.utils.FileUtil;

import com.estate.business.ManageProjectService;
import com.estate.business.PriceProjectService;
import com.estate.business.ProFileService;
import com.estate.vo.Vo;
import com.estate.vo.impl.ManageProjectVo;
import com.estate.vo.impl.PriceProjectVo;
import com.estate.vo.impl.ProFileVo;
import com.estate.vo.impl.UserInfoVo;

public class FileUploadAction extends EstateSupport implements
		ServletRequestAware, ServletResponseAware {

	private String method;
	private String proId;
	private String proName;
	private String proNum;
	private ProFileService proFileService;
	private ManageProjectService manageProjectService;
	private PriceProjectService priceProjectService;
	private String Filedata;
	private String Filename;
	private String FiledataFileName;
	private String saveFile;
	private String saveName;
	private String implTitle;
	private String implement;

	private int id;
	private int proCatagory;
	private String fileRealName;
	private String fileSavename;
	private String filePath;
	private UserInfoVo uploadUser;
	private Date uploadDate;
	private Date receiveDate;
	private String remark;
	private String fileCompany;
	private int deleteId;
	private int trailState;
	private int isMajor;

	private HttpServletRequest request;
	private HttpServletResponse response;

	public String execute() throws Exception {
		String page = "";
		System.out.println(method);
		if ("list".equals(method)) {
			this.getSession().setAttribute("proId", proId);
			this.getSession().setAttribute("proName", proName);
			this.getSession().setAttribute("proNum", proNum);
			this.getSession().setAttribute("proCatagory", proCatagory);
			page = "list";
		} else if ("upload".equals(method)) {
			File directory = new File(FileUtil.COMPANYSTUFF_FILE);
			saveName = FileUtil.getReplaceFileName(this.getFiledataFileName());
			saveFile = directory + File.separator + saveName;
			FileInputStream fis = new FileInputStream(this.getFiledata());

			if (!directory.exists()) {
				directory.mkdir();
			}

			FileOutputStream fos = new FileOutputStream(saveFile);
			byte[] buffer = new byte[1024];
			int length;
			while ((length = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, length);
			}
			fis.close();
			fos.close();
			System.out.println(proCatagory);
			String realName = this.getFiledataFileName();
			this.getSession().setAttribute("proId", proId);
			this.getSession().setAttribute("saveName", saveName);
			this.getSession().setAttribute("saveFile", saveFile);
			this.getSession().setAttribute("realName", realName);
			this.getSession().setAttribute("proCatagory", proCatagory);
			page = "upload";

		} else if ("write".equals(method)) {
			this.setProCatagory(Integer.parseInt(this.getSession()
					.getAttribute("proCatagory").toString()));
			java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
			ProFileVo vo = new ProFileVo();
			vo.setReceiveDate(receiveDate);
			vo.setFilePath(this.getSession().getAttribute("saveFile")
					.toString());
			vo.setUploadUser(this.getSessionUser());
			vo.setFileRealName(this.getSession().getAttribute("realName")
					.toString());
			vo.setUploadDate(date);
			vo.setFileSavename(this.getSession().getAttribute("saveName")
					.toString());
			vo.setFileCompany(this.getFileCompany());
			vo.setProId(Integer.parseInt(this.getSession()
					.getAttribute("proId").toString()));
			vo.setRemark(remark);
			vo.setProCatagory(this.getProCatagory());
			proFileService.addProFile(vo);

			this.getRequest().setAttribute("isNameVisible", new Boolean(true));
			this.getRequest().setAttribute("fileName",
					this.getSession().getAttribute("realName").toString());
			page = "upload";
		} else if ("download".equals(method)) {
			response.setContentType("application/x-download");
			String fileDownload = this.getFilePath();
			String filedisplay = this.getFileRealName();
			response.addHeader("Content-Disposition", "attachment;filename="
					+ filedisplay);

			OutputStream outp = null;
			FileInputStream in = null;

			try {
				outp = response.getOutputStream();
				in = new FileInputStream(fileDownload);

				byte[] b = new byte[1024];
				int i = 0;

				while ((i = in.read(b)) > 0) {
					outp.write(b, 0, i);
				}
				outp.flush();
			} catch (Exception e) {
				System.out.println("Error!");
				e.printStackTrace();
			} finally {
				if (in != null) {
					in.close();
					in = null;
				}
				if (outp != null) {
					outp.close();
					outp = null;
				}
			}
			page = null;
		} else if ("implement".equals(method)) {
			ManageProjectVo vo = (ManageProjectVo) manageProjectService
					.getManageProjectByPK(Integer.parseInt(proId));
			if (this.getProCatagory() == 31) {
				this.setImplTitle("实施方案");
				this.getSession()
						.setAttribute("implement", vo.getImplProgram());
			} else if (this.getProCatagory() == 32) {
				this.setImplTitle("实施计划");
				this.getSession().setAttribute("implement", vo.getImplPlan());
			}

			this.getSession().setAttribute("proId", proId);
			this.getSession().setAttribute("implTitle", this.getImplTitle());
			this.getSession().setAttribute("proName", proName);
			this.getSession().setAttribute("proNum", proNum);

			page = "implement";
		} else if ("implUpload".equals(method)) {
			this.setProCatagory(Integer.parseInt(this.getSession()
					.getAttribute("proCatagory").toString()));
			ManageProjectVo vo = (ManageProjectVo) manageProjectService
					.getManageProjectByPK(Integer.parseInt(this.getSession()
							.getAttribute("proId").toString()));
			if (this.getProCatagory() == 31) {
				vo.setImplProgram(this.getImplement());

			} else if (this.getProCatagory() == 32) {
				vo.setImplPlan(this.getImplement());
			}
			manageProjectService.updateManageProjectVo(vo);
			ProFileVo proFileVo = new ProFileVo();
			proFileVo.setFilePath(this.getSession().getAttribute("saveFile")
					.toString());
			proFileVo.setUploadUser(this.getSessionUser());
			proFileVo.setFileRealName(this.getSession()
					.getAttribute("realName").toString());
			proFileVo.setFileSavename(this.getSession()
					.getAttribute("saveName").toString());
			proFileVo.setProId(Integer.parseInt(this.getSession().getAttribute(
					"proId").toString()));
			proFileVo.setProCatagory(Integer.parseInt(this.getSession()
					.getAttribute("proCatagory").toString()));
			proFileService.addProFile(proFileVo);
			this.getRequest().setAttribute("isNameVisible", new Boolean(true));
			this.getRequest().setAttribute("fileName",
					this.getSession().getAttribute("realName").toString());

			page = "implement";
		} else if ("delete".equals(method)) {
			ProFileVo vo = this.getProFileService().getProFile(deleteId);
			this.setProId(((Integer) vo.getProId()).toString());
			this.setProCatagory(vo.getProCatagory());
			this.getProFileService().deleteProFile(this.getDeleteId());

			page = "delete";
		}  else if ("pricedelete".equals(method)) {
			ProFileVo vo = this.getProFileService().getProFile(deleteId);
			this.setProId(((Integer) vo.getProId()).toString());
			this.getProFileService().deleteProFile(this.getDeleteId());

			page = "pricedelete";
		} else if ("trailPlan".equals(method)) {
			this.setImplTitle("作业计划");
			this.setProCatagory(11);
			page = "joint";
		} else if ("trailSum".equals(method)) {
			this.setImplTitle("会议纪要");
			this.setProCatagory(12);
			page = "joint";
		} else if ("majorPlan".equals(method)) {
			this.setImplTitle("作业计划");
			this.setProCatagory(13);
			page = "joint";
		} else if ("majorSum".equals(method)) {
			System.out.println("in major");
			this.setImplTitle("会议纪要");
			this.setProCatagory(14);
			page = "joint";
		} else if ("jointUpload".equals(method)) {
			System.out.println("start joint");
			ProFileVo proFileVo = new ProFileVo();
			proFileVo.setFilePath(this.getSession().getAttribute("saveFile")
					.toString());
			proFileVo.setUploadUser(this.getSessionUser());
			proFileVo.setFileRealName(this.getSession()
					.getAttribute("realName").toString());
			proFileVo.setFileSavename(this.getSession()
					.getAttribute("saveName").toString());
			proFileVo.setProId(Integer.parseInt(this.getSession().getAttribute(
					"proId").toString()));
			proFileVo.setProCatagory(Integer.parseInt(this.getSession()
					.getAttribute("proCatagory").toString()));
			proFileService.addProFile(proFileVo);
			this.getRequest().setAttribute("isNameVisible", new Boolean(true));
			this.getRequest().setAttribute("fileName",
					this.getSession().getAttribute("realName").toString());
			this.getCatagoryInfo(Integer.parseInt(this.getSession()
					.getAttribute("proCatagory").toString()));
			System.out.println("aaa");
			page = "joint";
		} else if ("fileList".equals(method)) {
			if (this.getProCatagory() == 1
					|| (this.getProCatagory() >= 11 && this.getProCatagory() <= 14)) {
				PriceProjectVo vo = (PriceProjectVo) priceProjectService
						.findByPK(Integer.parseInt(this.getProId()));
				this.setIsMajor(vo.getIsMajor());
				this.setTrailState(vo.getTrailState());
				System.out.println(vo.getIsMajor());
				System.out.println(vo.getTrailState());
			}
			List<Vo> voList = proFileService.getFileList(this.getProCatagory(),
					Integer.parseInt(this.getProId()));
			if (this.getProCatagory() == 11) {
				voList.addAll(proFileService.getFileList(12, Integer
						.parseInt(this.getProId())));
			} else if (this.getProCatagory() == 13) {
				voList.addAll(proFileService.getFileList(14, Integer
						.parseInt(this.getProId())));
			} 
			this.getRequest().setAttribute("fileList", voList);
			page = "fileList";
		}
		return page;
	}

	public void getCatagoryInfo(int catagory) {
		switch (catagory) {
		case 11:
			this.setImplTitle("作业计划");
			this.setProCatagory(11);
			break;
		case 12:
			this.setImplTitle("会议纪要");
			this.setProCatagory(12);
			break;
		case 13:
			this.setImplTitle("作业计划");
			this.setProCatagory(13);
			break;
		case 14:
			this.setImplTitle("会议纪要");
			this.setProCatagory(14);
			break;
		}
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getProCatagory() {
		return proCatagory;
	}

	public void setProCatagory(int proCatagory) {
		this.proCatagory = proCatagory;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}

	public String getFileSavename() {
		return fileSavename;
	}

	public void setFileSavename(String fileSavename) {
		this.fileSavename = fileSavename;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public UserInfoVo getUploadUser() {
		return uploadUser;
	}

	public void setUploadUser(UserInfoVo uploadUser) {
		this.uploadUser = uploadUser;
	}

	public Date getUploadDate() {
		return uploadDate;
	}

	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}

	public Date getReceiveDate() {
		return receiveDate;
	}

	public void setReceiveDate(Date receiveDate) {
		this.receiveDate = receiveDate;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getFileCompany() {
		return fileCompany;
	}

	public void setFileCompany(String fileCompany) {
		this.fileCompany = fileCompany;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getMethod() {
		return method;
	}

	public void setProId(String proId) {
		this.proId = proId;
	}

	public String getProId() {
		return proId;
	}

	public void setProName(String proName) {
		this.proName = proName;
	}

	public String getproName() {
		return proName;
	}

	public void setProNum(String proNum) {
		this.proNum = proNum;
	}

	public String getProNum() {
		return proNum;
	}

	public void setProFileService(ProFileService proFileService) {
		this.proFileService = proFileService;
	}

	public ProFileService getProFileService() {
		return proFileService;
	}

	public void setFiledata(String filedata) {
		Filedata = filedata;
	}

	public String getFiledata() {
		return Filedata;
	}

	public void setFilename(String filename) {
		Filename = filename;
	}

	public String getFilename() {
		return Filename;
	}

	public void setFiledataFileName(String filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public String getFiledataFileName() {
		return FiledataFileName;
	}

	public void setSaveFile(String saveFile) {
		this.saveFile = saveFile;
	}

	public String getSaveFile() {
		return saveFile;
	}

	public void setSaveName(String saveName) {
		this.saveName = saveName;
	}

	public String getSaveName() {
		return saveName;
	}

	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}

	public void setImplTitle(String implTitle) {
		this.implTitle = implTitle;
	}

	public String getImplTitle() {
		return implTitle;
	}

	public void setImplement(String implement) {
		this.implement = implement;
	}

	public String getImplement() {
		return implement;
	}

	public void setManageProjectService(
			ManageProjectService manageProjectService) {
		this.manageProjectService = manageProjectService;
	}

	public ManageProjectService getManageProjectService() {
		return manageProjectService;
	}

	public void setDeleteId(int deleteId) {
		this.deleteId = deleteId;
	}

	public int getDeleteId() {
		return deleteId;
	}

	public void setTrailState(int trailState) {
		this.trailState = trailState;
	}

	public int getTrailState() {
		return trailState;
	}

	public void setIsMajor(int isMajor) {
		this.isMajor = isMajor;
	}

	public int getIsMajor() {
		return isMajor;
	}

	public void setPriceProjectService(PriceProjectService priceProjectService) {
		this.priceProjectService = priceProjectService;
	}

	public PriceProjectService getPriceProjectService() {
		return priceProjectService;
	}

}
